/*******************************************************************************
 * Copyright (c) 2011 TXT e-solutions SpA
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * This work was performed within the IoT_at_Work Project
 * and partially funded by the European Commission's
 * 7th Framework Programme under the research area ICT-2009.1.3
 * Internet of Things and enterprise environments.
 *
 *
 * Authors:
 *     Cristoforo Seccia (TXT e-solutions SpA)
 *
 * Contributors:
 *      Domenico Rotondi (TXT e-solutions SpA)
 *******************************************************************************/
package it.txt.access.capability.demo.client.tcp;

import it.txt.access.capability.commons.signer.model.X509CertificateSubjectInfo;
import it.txt.access.capability.demo.client.view.model.ServerResponseInfoModel;
import it.txt.access.capability.demo.schema.CapabilityResponseType;
import it.txt.access.capability.demo.schema.factory.CapabilityDemoFactoryException;
import it.txt.access.capability.demo.schema.response.factory.CapabilityDemoResponseFactory;
import it.txt.access.capability.commons.signer.X509DocumentSigner;
import it.txt.access.capability.commons.utils.XMLPrinter;

import java.io.InputStream;
import java.security.GeneralSecurityException;
import org.w3c.dom.Document;

/**
 *
 * @author Cristoforo Seccia (TXT e-solutions SpA)
 */
public class RequestClientHelper {

    public static ServerResponseInfoModel recoverServiceResponseInfo(final InputStream inputStream) throws ResponseClientException{
        CapabilityResponseType capabilityResponse = null;
        X509CertificateSubjectInfo subjectInfo = new X509CertificateSubjectInfo();
        ServerResponseInfoModel infoModel = new ServerResponseInfoModel();
        try {
            capabilityResponse = CapabilityDemoResponseFactory.getInstance().createResponse(inputStream);
            Document responseDoc = CapabilityDemoResponseFactory.getInstance().createResponseDocument(capabilityResponse);
            XMLPrinter.logDocumentElement(responseDoc.getDocumentElement(), "Received Capability response", false);
            // Check the Capability Response has not been tampered!
            if (!X509DocumentSigner.verifyXMLElementSign(
                    responseDoc.getDocumentElement(), subjectInfo)) {
                throw new ResponseClientException("The Capability Response Document Sign is Invalid");
            }
        } catch (CapabilityDemoFactoryException ex) {
            throw new ResponseClientException("Error while recovering Capability Response.", ex);
        } catch(GeneralSecurityException ex) {
            throw new ResponseClientException("Error while recovering Capability Response.", ex);
        }
        infoModel.setResponseID(capabilityResponse.getCapabilityResponseID());
        infoModel.setServiceResponseStatus(capabilityResponse.getStatus());
        infoModel.setResponseSigner(subjectInfo.getX509SubjectName());
        infoModel.setSignerKeyIssuer(subjectInfo.getX509IssuerName());
        infoModel.setSignerKeyID(subjectInfo.getX509SerialNumber().toString(16));
        return infoModel;
    }
}
